package com.deer.wms.instock.service.enter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.instock.service.enter.EnterErp;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: xu
 * @Date: 2022/06/20/11:15
 * @Description: 702 来料检验申请单，委外入库
 */
@Component

public class Out implements EnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;



    @Override
    public ErpType gainType() {
        return new ErpType(702, 5);
    }

    @Override
    public Map<String, String> EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails, BaseAssist enterAssist) {
//        List<AsnDetail> newAsnDetail = asnDetails.stream().filter(item -> item.getErpStockId() != 471713).collect(Collectors.toList());

//        List<AsnDetail> newAsnDetail = asnDetails.stream().filter(item -> ObjectUtil.isEmpty(item.getErpStockId()) || item.getErpStockId() != 471713).collect(Collectors.toList());
//        过滤 VMI 跟直发虚仓
        List<AsnDetail> newAsnDetail =asnDetails.stream().filter(item -> ObjectUtil.isEmpty(item.getErpStockId())
                                || !pushErpService.getNotAasnStock().contains(item.getErpStockId())).collect(Collectors.toList());


        if (ObjectUtil.isEmpty(newAsnDetail)) {
            return null;
        }
//        List<AsnDetail> newAsnDetail = asnDetails.stream().filter(item -> ObjectUtil.isEmpty(item.getStockId()) || item.getStockId() != 471713).collect(Collectors.toList());

        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(asnMaster.getAsnType());
        orderToErpBO.setSourceBillTypeId(asnMaster.getBillType());
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("INPUT"));
        for (AsnDetail asnDetail : asnDetails) {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setSourceBillId(asnDetail.getSourceId()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(asnDetail.getRowIndex()); //行号
            orderDetailBO.setBatchNo(asnDetail.getBatchName()); //批号
            orderDetailBO.setQty(asnDetail.getQty()); //数量
            orderDetailBO.setStockId(asnDetail.getStockId()); //
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);
        return pushErpService.pushIMBIll(orderToErpBO);
    }
}
